// pages/shanghjhexiao/shanghjhexiao.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wordKey: '',
    jihuo: '',
    mealsList: '',
    xinxi: true,
    length: '', //数据长度
    imgs: [],
    count: 3,
    wangdian: [{
        id: '100',
        wangname: "100元优惠券（存款5万以上）"
      },
      {
        id: '200',
        wangname: "200元优惠券（存款10-20万）"
      },
      {
        id: '500',
        wangname: "500元优惠券（存款20万以上）"
      },

    ],
    wangdiany: true,
    shurukuang: true,
    POST_ADD: '',
    MARKETING_AMOUNT: '', //'营销金额',
    login_id: '', //支付宝账号
    login_zhifu: '', //支付宝账号
    MARKETING_NOTE: '',
    flag: true, //设置节流
  },




  showPopup() {
    this.setData({
      wangdiany: false,
    })
  },

  xwang(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.wangname);
    console.log(e.currentTarget.dataset.item.id);
    this.setData({
      POST_ADD: e.currentTarget.dataset.item.wangname,
      wangdiany: true,
      MARKETING_AMOUNT: e.currentTarget.dataset.item.id,
    })


  },



  search_but() {
    this.setData({
      mealsList: app.globalData.mealsList
    });
    var that = this
    if (that.data.wordKey == "") {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/shangluoTicket/API_4813_unActivationTicketprize',
        method: 'POSt',
        data: {
          VALID: 0,
          USER_ASSETS_TICKET_PHONE: that.data.wordKey,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          that.setData({
            jihuo: res.data.data,
            xinxi: false,
          })
        }
      })
    }
  },


  //提交信息


  tijiaoxingxi() {

    var that = this

    if (this.data.flag) {

      this.data.flag = false

      if (that.data.login_id == '' || that.data.login_zhifu == '' || that.data.MARKETING_AMOUNT == '' || that.data.MARKETING_NOTE == '') {
        wx.showToast({
          title: "请完善信息",
          icon: "error",
          duration: 2000
        })
      } else if (that.data.login_id != that.data.login_zhifu) {
        wx.showToast({
          title: "两次账号不一致",
          icon: "error",
          duration: 2000
        })
      } else {

        wx.request({
          url: app.globalData.interface+'/shangluoTicket/API_4812_ticketPrizeMarketing',
          method: 'POSt',
          data: {

            USER_ASSETS_TICKET_PHONE: that.data.wordKey,
            USER_PRIZE_ID: that.data.jihuo.USER_PRIZE_ID,
            EMPLOYEE_ID: that.data.mealsList,
            MARKETING_AMOUNT: that.data.MARKETING_AMOUNT,
            login_id: that.data.login_id,
            MARKETING_NOTE: that.data.MARKETING_NOTE,
            KEY: 'xianjgjKey9999',
            OPEN_ID: this.data.openid, //用户OPEN_ID
            session_key: this.data.session_key,
            OPEN_UUID: this.data.jiami
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res.data);
            if (res.data.result == "error") {
              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
              this.setData({
                shurukuang: true,
              })
            } else {

              wx.showModal({
                title: '温馨提示',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) { //这里是点击了确定以后
                  } else { //这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
              this.setData({
                shurukuang: true,
              })
            }
          }
        })

      }

      setTimeout(() => {
        this.data.flag = true
      }, 4500)
    }
  },



  buttonji() {
    var that = this
    wx.request({
      url: app.globalData.interface+'/shangluoTicket/API_4811_activationTicketprize',
      method: 'POSt',
      data: {
        VALID: '1',
        USER_ASSETS_TICKET_PHONE: that.data.wordKey,
        USER_PRIZE_ID: that.data.jihuo.USER_PRIZE_ID,
        EMPLOYEE_ID: that.data.mealsList,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data);
        if (res.data.result == "error") {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
              }
            }
          })
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后

                that.setData({
                  xinxi: true,
                  wordKey: '',

                })


              } else { //这里是点击了取消以后

                that.setData({
                  xinxi: true,
                  wordKey: '',

                })

                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },

  // url: 'http://192.168.3.173:8081/newVersion/API_2031_indexData',





  //点击显示
  // buttonji: function (e) {

  //   setTimeout(function () {
  //     this.buttonjichenggong()
  //   }, 1000);




    // console.log(111);
    // switch (this.data.imgs.length) {
    //   case 0:
    //     this.data.count = 3
    //     break
    //   case 1:
    //     this.data.count = 2
    //     break
    //   case 2:
    //     this.data.count = 1
    //     break
    // }
    // var that = this
    // wx.chooseImage({
    //   count: that.data.count, // 默认3
    //   sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
    //   sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
    //   success: function (res) {
    //     console.log(res);
    //     console.log(res.tempFilePaths);
    //     console.log(that.data.mealsList);
    //     // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
    //     var tempFilePaths = res.tempFilePaths
    //     for (var i = 0; i < tempFilePaths.length; i++) {
    //       //限制上传图片大小（2M）
    //       if (res.tempFiles[i].size > 4500000) {
    //         wx.showModal({
    //           content: '上传图片不能大于4M!',
    //           showCancel: false,
    //         });
    //         return false;
    //       } else {
    //         wx.uploadFile({
    //           url: app.globalData.interface+'/shanyouhui/API_1097_uploadShangLuoFileCOS',

    //           method: 'POST',
    //           name: "FIMG",
    //           filePath: tempFilePaths[i],
    //           formData: {
    //             EMPLOYEE_ID: that.data.mealsList,
    //             KEY: 'xianjgjKey9999',
    //             OPEN_ID: that.data.openid, //用户OPEN_ID
    //             session_key: that.data.session_key,
    //             OPEN_UUID: that.data.jiami
    //           },
    //           header: {
    //             "content-type": "multipart/form-data"
    //           },
    //           success: function (res) {
    //             // USER_COUPON_PHONE
    //             console.log(res);

    //             // console.log('______',  );
    //             if (JSON.parse(res.data).result == 'success') {
    //               wx.showToast({
    //                 title: "上传成功",
    //                 icon: "none",
    //                 duration: 1500
    //               })
    //               setTimeout(function () {
    //                 that.buttonjichenggong()
    //               }, 2000);
    //             }
    //           },
    //           fail: function (err) {
    //             console.log(err);
    //             wx.showToast({
    //               title: "上传失败",
    //               icon: "none",
    //               duration: 2000
    //             })
    //             wx.redirectTo({
    //               url: '../../pages/shanghjhexiao/shanghjhexiao',
    //             })
    //           },
    //           complete: function (result) {
    //             console.log(result.errMsg)
    //           }
    //         })
    //       }
    //     }
    //   }
    // })

  // },







  //营销
  yinxiao: function (e) {


    
    console.log(111);
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 3
        break
      case 1:
        this.data.count = 2
        break
      case 2:
        this.data.count = 1
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res);
        console.log(res.tempFilePaths);
        console.log(that.data.mealsList);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          //限制上传图片大小（2M）
          if (res.tempFiles[i].size > 4500000) {
            wx.showModal({
              content: '上传图片不能大于4M!',
              showCancel: false,
            });
            return false;
          } else {
            wx.uploadFile({
              url: app.globalData.interface+'/shanyouhui/API_1097_uploadShangLuoFileCOS',

              method: 'POST',
              name: "FIMG",
              filePath: tempFilePaths[i],
              formData: {
                EMPLOYEE_ID: that.data.mealsList,
                KEY: 'xianjgjKey9999',
                OPEN_ID: this.data.openid, //用户OPEN_ID
                session_key: this.data.session_key,
                OPEN_UUID: this.data.jiami
              },
              header: {
                "content-type": "multipart/form-data"
              },
              success: function (res) {
                // USER_COUPON_PHONE
                console.log(res);

                // console.log('______',  );
                if (JSON.parse(res.data).result == 'success') {
                  wx.showToast({
                    title: "上传成功",
                    icon: "none",
                    duration: 1500
                  })



                  setTimeout(function () {
                    that.setData({
                      shurukuang: false,
                    })
                  }, 1000);



                }
              },
              fail: function (err) {
                console.log(err);
                wx.showToast({
                  title: "上传失败",
                  icon: "none",
                  duration: 2000
                })
                wx.redirectTo({
                  url: '../../pages/shanghjhexiao/shanghjhexiao',
                })
              },
              complete: function (result) {
                console.log(result.errMsg)
              }
            })
          }
        }
      }
    })

  },


















  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    console.log(value);



    this.setData({
      mobile: value,
    })

    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        wordKey: '',
        mobileFormat: false,
        xinxi: true,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        wordKey: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        wordKey: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})